import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:palmf_hid/common/entities/data_base/my_database.dart';
import 'package:palmf_hid/common/utils/key_board_util.dart';
import 'package:palmf_hid/common/utils/utils.dart';
import 'package:palmf_hid/common/values/values.dart';
import 'package:palmf_hid/pages/main/check/sync_data_select/my_input_widget.dart';

import 'add_pet_logic.dart';
import 'add_pet_state.dart';

class AddPetPage extends StatefulWidget {
  PetInfo? petInfo;
  String type;

  AddPetPage({Key? key, required this.petInfo, required this.type})
      : super(key: key);

  @override
  State<AddPetPage> createState() => _AddPetPageState(this.petInfo, this.type);
}

class _AddPetPageState extends State<AddPetPage> {
  final AddPetLogic logic = Get.put(AddPetLogic());
  final AddPetState state = Get.find<AddPetLogic>().state;

  _AddPetPageState(PetInfo? petInfo, String type) {
    logic.operateType = type;

    if (null == petInfo) {
      return;
    }
    logic.handlePetInfo(petInfo);

  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      child: Obx(() => Container(
          height: 0.65.sh,
          width: 0.3.sw,
          padding: const EdgeInsets.symmetric(horizontal: 15),
          decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(10))),
          child: Column(
            children: [
              const Row(
                children: [
                  Text(
                    "新增患者信息",
                    style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
                  ),
                  Spacer(),
                  CloseButton(),
                ],
              ),
              const Divider(),
              _buildPetName("请输入同步条数"),
              const SizedBox(
                height: 10,
              ),
              _buildMasterName("请输入主人姓名"),
              const SizedBox(
                height: 10,
              ),
              _buildPhone("请输入联系电话"),
              const SizedBox(
                height: 10,
              ),
              _buildSex(),
              const SizedBox(
                height: 10,
              ),
              _buildAge(""),
              const SizedBox(
                height: 10,
              ),
              _buildPetWeight(""),
              const SizedBox(
                height: 10,
              ),
              _buildPetType(),
              const SizedBox(
                height: 30,
              ),
              _buildOperate()
            ],
          ))),
    );
  }

  ///宠物名称
  _buildPetName(String str) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        const Text("宠物名称"),
        const SizedBox(
          width: 5,
        ),
        SizedBox(
          height: 30,
          width: 0.2.sw,
          child: windowsClearTextField(
            textEditingController: logic.petNameTextEditingController,
            child: inputContainer(
              hintText: str,
              keyboardType: TextInputType.text,
              maxLength: 20,
              maxLines: 1,
              // inputFormatters: [
              //   FilteringTextInputFormatter.allow(RegExp(r'[0-9]')) //设置只允许输入数字
              // ],
              controller: logic.petNameTextEditingController,
              focusNode: logic.petNameFocusNode,
              onChange: (str) {
                MyLogUtil().i(str);
                state.petName = str;
              },
              close: state.petName == ""
                  ? const SizedBox()
                  : InkWell(
                      child: IconButton(
                          onPressed: () {
                            state.petName = "";
                            logic.petNameTextEditingController.text = "";
                          },
                          icon: const Icon(
                            Icons.close,
                            size: 18,
                          )),
                    ),
            ),
          ),
        )
      ],
    );
  }

  ///主人名称
  _buildMasterName(String str) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        const Text("主人姓名"),
        const SizedBox(
          width: 5,
        ),
        SizedBox(
          height: 30,
          width: 0.2.sw,
          child: windowsClearTextField(
            textEditingController: logic.masterNameTextEditingController,
            child: inputContainer(
              hintText: str,
              keyboardType: TextInputType.text,
              maxLength: 10,
              maxLines: 1,
              // inputFormatters: [
              //   FilteringTextInputFormatter.allow(RegExp(r'[0-9]')) //设置只允许输入数字
              // ],
              controller: logic.masterNameTextEditingController,
              focusNode: logic.masterNameFocusNode,
              onChange: (str) {
                state.masterName = str;
              },
              close: state.masterName == ""
                  ? const SizedBox()
                  : InkWell(
                      child: IconButton(
                          onPressed: () {
                            state.masterName = "";
                            logic.masterNameTextEditingController.text = "";
                          },
                          icon: const Icon(
                            Icons.close,
                            size: 18,
                          )),
                    ),
            ),
          ),
        )
      ],
    );
  }

  ///联系电话
  _buildPhone(String str) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        const Text("联系电话"),
        const SizedBox(
          width: 5,
        ),
        SizedBox(
          height: 30,
          width: 0.2.sw,
          child: windowsClearTextField(
            textEditingController: logic.phoneTextEditingController,
            child: inputContainer(
              hintText: str,
              keyboardType: TextInputType.phone,
              maxLength: 12,
              maxLines: 1,
              inputFormatters: [
                FilteringTextInputFormatter.allow(RegExp(r'[0-9]')) //设置只允许输入数字
              ],
              controller: logic.phoneTextEditingController,
              focusNode: logic.phoneFocusNode,
              onChange: (str) {
                MyLogUtil().i(str);
                state.phone = str;
              },
              close: state.phone == ""
                  ? const SizedBox()
                  : InkWell(
                      child: IconButton(
                          onPressed: () {
                            state.phone = "";
                            logic.phoneTextEditingController.text = "";
                          },
                          icon: const Icon(
                            Icons.close,
                            size: 18,
                          )),
                    ),
            ),
          ),
        )
      ],
    );
  }

  ///性别
  _buildSex() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        const Text(
          "        性别",
        ),
        const SizedBox(
          width: 10,
        ),
        DropdownButton(
            alignment: Alignment.center,
            icon: const Icon(Icons.arrow_drop_down),
            style: const TextStyle(color: MyColor.c_FF999999, fontSize: 14),
            iconSize: 30,
            iconEnabledColor: Colors.black54.withOpacity(0.7),
            hint: const Text('请选择性别'),
            value: state.petSex == "" ? null : state.petSex,
            items: const [
              DropdownMenuItem(child: Text('雄'), value: '雄'),
              DropdownMenuItem(child: Text('雌'), value: '雌')
            ],
            onChanged: (value) {
              state.petSex = value ?? "";
            })
      ],
    );
  }

  ///年龄
  _buildAge(String str) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        const Text("       年龄"),
        const SizedBox(
          width: 5,
        ),
        SizedBox(
          height: 30,
          width: 0.1.sw - 30,
          child: windowsClearTextField(
            textEditingController: logic.petYearTextEditingController,
            child: inputContainer(
              hintText: str,
              keyboardType: TextInputType.number,
              maxLength: 2,
              maxLines: 1,
              inputFormatters: [
                FilteringTextInputFormatter.allow(RegExp(r'[0-9]')) //设置只允许输入数字
              ],
              controller: logic.petYearTextEditingController,
              focusNode: logic.petYearFocusNode,
              onChange: (str) {
                MyLogUtil().i(str);
                state.petYear = str;
              },
            ),
          ),
        ),
        const SizedBox(
          width: 5,
        ),
        const Text("岁"),
        const SizedBox(
          width: 5,
        ),
        SizedBox(
          height: 30,
          width: 0.1.sw - 30,
          child: windowsClearTextField(
            textEditingController: logic.petMonthTextEditingController,
            child: inputContainer(
              hintText: str,
              keyboardType: TextInputType.number,
              maxLength: 2,
              maxLines: 1,
              inputFormatters: [
                FilteringTextInputFormatter.allow(RegExp(r'[0-9]')) //设置只允许输入数字
              ],
              controller: logic.petMonthTextEditingController,
              focusNode: logic.petMonthFocusNode,
              onChange: (str) {
                MyLogUtil().i(str);
                state.petMonth = str;
              },
            ),
          ),
        ),
        const SizedBox(
          width: 5,
        ),
        const Text("月"),
      ],
    );
  }

  ///体重
  _buildPetWeight(String str) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        const Text(" 体重(kg)"),
        const SizedBox(
          width: 5,
        ),
        SizedBox(
          height: 30,
          width: 0.2.sw,
          child: windowsClearTextField(
            textEditingController: logic.petWeightTextEditingController,
            child: inputContainer(
              hintText: str,
              keyboardType: TextInputType.number,
              maxLength: 3,
              maxLines: 1,
              inputFormatters: [
                FilteringTextInputFormatter.allow(RegExp(r'[0-9]')) //设置只允许输入数字
              ],
              controller: logic.petWeightTextEditingController,
              focusNode: logic.petWeightFocusNode,
              onChange: (str) {
                MyLogUtil().i(str);
                state.petWeight = str;
              },
              close: state.petWeight == ""
                  ? const SizedBox()
                  : InkWell(
                      child: IconButton(
                          onPressed: () {
                            state.petWeight = "";
                            logic.petWeightTextEditingController.text = "";
                          },
                          icon: const Icon(
                            Icons.close,
                            size: 18,
                          )),
                    ),
            ),
          ),
        )
      ],
    );
  }

  ///宠物类别
  _buildPetType() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        const Text(
          "宠物类别",
        ),
        const SizedBox(
          width: 10,
        ),
        DropdownButton(
            alignment: Alignment.center,
            icon: const Icon(Icons.arrow_drop_down),
            style: const TextStyle(color: MyColor.c_FF999999, fontSize: 14),
            iconSize: 30,
            iconEnabledColor: Colors.black54.withOpacity(0.7),
            hint: const Text('请选择类别'),
            value: state.petType == "" ? null : state.petType,
            items: const [
              DropdownMenuItem(child: Text('猫'), value: '猫'),
              DropdownMenuItem(child: Text('狗'), value: '狗')
            ],
            onChanged: (value) {
              state.petType = value ?? "";
            })
      ],
    );
  }

  @override
  void dispose() {
    Get.delete<AddPetLogic>();
    super.dispose();
  }

  _buildOperate() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        InkWell(
          child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 5),
              decoration: BoxDecoration(
                  color: Colors.grey.shade400,
                  borderRadius: const BorderRadius.all(Radius.circular(5))),
              child: const Text("取消",
                  style: TextStyle(color: MyColor.c_FFFFFFFF, fontSize: 12))),
          onTap: () {
            Get.back();
          },
        ),
        InkWell(
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 5),
            decoration: const BoxDecoration(
                color: MyColor.account_color,
                borderRadius: BorderRadius.all(Radius.circular(5))),
            child: const Text("确认",
                style: TextStyle(color: MyColor.c_FFFFFFFF, fontSize: 12)),
          ),
          onTap: () {
            logic.handleAddPet();
          },
        ),
      ],
    );
  }
}
